package com.cxrj.zhsq.userManage.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.administrate.dao.PersonnelMapper;
import com.cxrj.zhsq.administrate.dao.PropertyMapper;
import com.cxrj.zhsq.administrate.dao.VehicleMapper;
import com.cxrj.zhsq.administrate.entity.Property;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.dao.DirectoriesMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.userManage.entity.Directories;
import com.cxrj.zhsq.userManage.service.AppUserService;
import com.cxrj.zhsq.utils.Encrypt;

/**
 * @author : yuandi
 * @date 创建时间：2018年2月28日 下午2:50:49
 * @description
 * @return
 */
@Service
public class AppUserServiceImpl implements AppUserService {

	@Resource
	private AppUserMapper appUserMapper;

	@Resource
	private DirectoriesMapper directoriesMapper;

	@Resource
	private PersonnelMapper personnelMapper;

	@Resource
	private PropertyMapper propertyMapper;

	@Resource
	private VehicleMapper vehicleMapper;

	@Override
	public Map<String, Object> selectAppUser(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		String kod = request.getParameter("kod");

		sqlMap.put("start", Integer.parseInt(start));
		if (kod == null || kod == "") {
			sqlMap.put("condition", " where 1=1 ");
			recordsTotal = appUserMapper.getAppUserCount(" 1=1") + "";
		} else {
			sqlMap.put("condition", "  where  user_mobile like " + '"' + '%' + kod + '%' + '"' + " or user_name like " + '"' + '%' + kod + '%' + '"' + " or user_sex like " + '"' + '%' + kod + '%'
					+ '"' + " or community_name like " + '"' + '%' + kod + '%' + '"' + " ");
			recordsTotal = appUserMapper.getAppUserCount("user_mobile like " + '"' + '%' + kod + '%' + '"' + " or user_name like " + '"' + '%' + kod + '%' + '"' + " or user_sex like " + '"' + '%'
					+ kod + '%' + '"' + " or community_name like " + '"' + '%' + kod + '%' + '"' + "") + "";
		}

		sqlMap.put("length", Integer.parseInt(length));
		List<Map<String, Object>> list = appUserMapper.selectAppUserPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectAppUserById(String userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("tel", "");
		AppUser appUser = new AppUser();
		appUser = appUserMapper.selectAppUserById(userId);
		List<Directories> list = directoriesMapper.selectDirectories("1");
		for (Directories directories : list) {
			if (directories.getId().equals("4318bba7-ccd0-4444-b505-0f26c3651f24")) {
				resultMap.put("tel", directories.getTel());
			}
		}
		resultMap.put("appUser", appUser);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateAppUserByIds(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = appUserMapper.updateAppUserByIds(Arrays.asList(id.split(",")));
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAppUser(AppUser appUser) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("userMobile", appUser.getUserMobile());
		sqlMap.put("userName", appUser.getUserName());
		sqlMap.put("userIdcard", appUser.getUserIdcard());
		sqlMap.put("tel", appUser.getUserMobile());
		sqlMap.put("name", appUser.getUserName());
		sqlMap.put("idCard", appUser.getUserIdcard());
		// String[] addresss = appUser.getUserAddress().split("-");
		// sqlMap.put("building", addresss[0]);
		// sqlMap.put("unit", addresss[1]);
		// sqlMap.put("doorCard", addresss[2]);
		Integer c1 = personnelMapper.selectByAppUser(sqlMap);
		if (c1 == 0) {
			resultMap.put("result", "2");
			return resultMap;
		}
		// Integer c2 = propertyMapper.selectByAppUser(sqlMap);
		// if (c2 == 0) {
		// resultMap.put("result", "3");
		// return resultMap;
		// }
		if (appUserMapper.selectByTel(sqlMap) != 0) {
			resultMap.put("result", "4");
			return resultMap;
		}
		List<Property> list = propertyMapper.selectByPersonnel(sqlMap);
		String addrs = "";
		for (Property property : list) {
			addrs += "," + property.getBuilding() + "-" + property.getUnit() + "-" + property.getDoorCard();
		}
		appUser.setUserId(UUID.randomUUID().toString());
		appUser.setUserStatus("1");
		appUser.setUserAccount(appUser.getUserMobile());
		if (!addrs.equals("")) {
			appUser.setUserAddress(addrs.substring(1));
		}
		Integer c = appUserMapper.insertSelective(appUser);
		resultMap.put("result", "0");
		if (c > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectLogin(String userAccount, String userPassword, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("tel", "");
		AppUser appUser = new AppUser();
		int result = 0;// 0:账号不存在，1:密码错误，2:登录成功
		// 用户名查询
		List<AppUser> user_mobile = appUserMapper.selectByCondition(" user_account='" + userAccount + "'");
		if (user_mobile != null && user_mobile.size() > 0) {
			// 查询密码
			List<AppUser> user_password = appUserMapper.selectByCondition(" user_account='" + userAccount + "' AND USER_PASSWORD=md5('" + userPassword + "')");
			if (user_password != null && user_password.size() > 0) {
				result = 2;// 登录成功
				request.getSession().setAttribute("user", user_password.get(0));
				appUser = user_password.get(0);
				if (appUser.getUserStatus().equals("0")) {
					result = 3;
					List<Directories> list = directoriesMapper.selectDirectories("1");
					for (Directories directories : list) {
						if (directories.getId().equals("4318bba7-ccd0-4444-b505-0f26c3651f24")) {
							resultMap.put("tel", directories.getTel());
						}
					}
				}
			} else {
				result = 1;// 密码错误
			}
		} else {
			result = 0;// 账号不存在
		}
		resultMap.put("result", result);
		resultMap.put("appUser", appUser);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateUser(AppUser appUser) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (appUser.getUserPassword() != null && !appUser.getUserPassword().equals("")) {
			appUser.setUserPassword(Encrypt.md5(appUser.getUserPassword()));
		}
		Integer c = appUserMapper.updateByPrimaryKeySelective(appUser);
		resultMap.put("result", "0");
		if (c > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updatePass(String oldPass, String newPass, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		if (!Encrypt.md5(oldPass).equals(appUser.getUserPassword())) {
			resultMap.put("result", "2");
			return resultMap;
		}
		appUser.setUserPassword(Encrypt.md5(newPass));
		Integer c = appUserMapper.updateByPrimaryKeySelective(appUser);
		resultMap.put("result", "0");
		if (c > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateUserForget(String newPass, String userMobile) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectAppUserByPhone(userMobile);
		appUser.setUserPassword(Encrypt.md5(newPass));
		Integer c = appUserMapper.updateByPrimaryKeySelective(appUser);
		resultMap.put("result", "0");
		if (c > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateUserMobile(String pass, String userMobile, String userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		sqlMap.put("card", appUser.getUserIdcard());
		sqlMap.put("tel", userMobile);
		if (!Encrypt.md5(pass).equals(appUser.getUserPassword())) {
			resultMap.put("result", "2");
			return resultMap;
		}
		personnelMapper.updateByCard(sqlMap);
		vehicleMapper.updateByCard(sqlMap);
		propertyMapper.updateByCard(sqlMap);
		appUser.setUserAccount(userMobile);
		appUser.setUserMobile(userMobile);
		Integer c = appUserMapper.updateByPrimaryKeySelective(appUser);
		resultMap.put("result", "0");
		if (c > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectAllAppUser() {
		Map<String, Object> resultMap = new HashMap<>();
		List<AppUser> list = appUserMapper.selectAllAppUser();
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateAppUserByThaw(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = appUserMapper.updateAppUserByThaw(Arrays.asList(id.split(",")));
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}
}
